// 保存棋盘格子
import { ecs } from '../framework/assets/libs/ecs/ECS';
import { RandomManager } from "../framework/assets/core/common/manager/RandomManager";
import { cellList } from "../cccomponent/enum";
import { Node } from 'cc';
import { CellViewTurnComponent, EffectLayerTurnComponent } from "./utilsComponent";

@ecs.register('GridModel') // GridModel 可以自动挂载到 entity 上
export class GridModelComponent extends ecs.Comp {
    // 所有格子的实体
    cells: Array<Array<{ cellEntity: ecs.Entity }>> = [];
    // 可创建的 cell 类型
    cellCreateType: string[] = [];
    // grid 的节点
    gridNode: Node;
    // 动效层节点
    effectLayer: Node;

    // 为 cell 添加 moveComponent
    private moveCell: Map<number, ecs.Entity>;
    private processed: number = 0;

    // 动效
    private effects: Map<number, ecs.Entity>;

    init() {
        this.moveCell = new Map<number, ecs.Entity>();
        this.effects = new Map<number, ecs.Entity>();
    }

    reset() {
        this.cells = [];
        this.processed = 0;
        this.moveCell = new Map<number, ecs.Entity>();
        this.effects = new Map<number, ecs.Entity>();
    }

    // 随机选择一个类型
    getRandomCellType() {
        const index = Math.floor(Math.random() * this.cellCreateType.length);
        return this.cellCreateType[index];
    }

    // 随机选择类型列表
    randomCellTypeList(selectNum?: number) {
        if (!selectNum || selectNum > cellList.length || selectNum < 1) {
            // 默认选择 5 个
            selectNum = 5;
        }
        this.cellCreateType = RandomManager.instance.getRandomByObjectList(cellList, selectNum);
    }

    // 交换 2 格子
    exchangeCell(ax, ay, bx, by) {
        [ this.cells[ax][ay], this.cells[bx][by] ] = [ this.cells[bx][by], this.cells[ax][ay] ];
    }

    // 允许处理格子
    allowCellMove(entity: ecs.Entity) {
        this.moveCell.set(entity.eid, entity);
    }

    addEffect(entity: ecs.Entity) {
        this.effects.set(entity.eid, entity);
    }

    // 统一提交 cell 处理
    commitMovement() {
        for (const e of this.moveCell.values()) {
            console.log('commit eid', e.eid);
            e.add(CellViewTurnComponent);
        }
        // 允许动效
        for (const e of this.effects.values()) {
            const comp = e.add(EffectLayerTurnComponent);
            // 添加参数
            comp.parent = this.effectLayer;
        }
    }

    // 处理完 cell
    isResolve() {
        this.processed++;
        if (this.processed === this.moveCell.size) {
            this.processed = 0;
            this.moveCell.clear();
            this.effects.clear();
            console.log('all cell resolved');
            return true;
        }
        return false;
    }

    // 清空本次操作
    cleanCell() {
        this.processed = 0;
        // 清空没有其它组件的cell
        this.moveCell.clear();
    }

    // 是否有格子可以移动
    isCellCanMove() {
        return this.moveCell.size > 0;
    }

    // 所有待处理的格子
    get totalCell() {
        return this.moveCell.size;
    }

    // 根据索引获取 entity
    getCellByIndex(index: number) {
        let i = 0;
        for (const k of this.moveCell.keys()) {
            if (i === index) {
                return this.moveCell[k];
            }
        }
        return null;
    }
}
